Ext.namespace('VAS');

VAS.PropertyConfigWindow = function($config) {
	var _this = this;
	
	this.postData = {};
	
	this.doChecked  = function(obj) {
		var _records = obj.getSelectionModel().getSelections();   	
	 	if (typeof(_records)==='undefined'||_records.length==0) {
	 		Ext.Msg.alert('提示','您没有选择数据');
	 		return false;
	 	} else if (typeof(_records)==='object' && _records.length>1) {
	 		Ext.Msg.alert('提示','每次操作只能选择一条数据');
	 		return false;
	 	}  
		var  _record;
		this.postData = {};
		for (var i=0;i<_records.length;i++) {
			_record = _records[i];	
			Ext.apply(this.postData,{"id":_record.get("id")});
		}	
		
	 	return true;
	}
	
    var navHandler = function(direction){
    
        var wizard = Ext.getCmp('wizard').layout;
        var prev = Ext.getCmp('move-prev');
        var next = Ext.getCmp('move-next');
        var activeId = wizard.activeItem.id;
        
        if (activeId == 'card-0') {
            var obj = Ext.getCmp($config.children[1].gridPanel.id);
            // 下一步
            if (direction == 1 && this.doChecked(obj)) {
            	var nextPanel = Ext.getCmp($config.children[2].gridPanel.id);
            	var tempId  = nextPanel.store.baseParams.id;
			 	Ext.apply(nextPanel.store.baseParams,_this.postData);
		 	    wizard.setActiveItem(1);
		 	    if (tempId != undefined  &&  tempId != _this.postData.id) {
		 	    	nextPanel.store.reload();
		 	    }
		        prev.setDisabled(false);   
            }
        } else if (activeId == 'card-1') {
        	var obj = Ext.getCmp($config.children[2].gridPanel.id);
        	// 下一步
            if (direction == 1 && this.doChecked(obj)) {
            	var nextPanel = Ext.getCmp($config.children[3].gridPanel.id);
            	var tempId  = nextPanel.store.baseParams.id;
			 	Ext.apply(nextPanel.store.baseParams,_this.postData);
                wizard.setActiveItem(2);
		 	    if (tempId != undefined  &&  tempId != _this.postData.id) {
		 	    	nextPanel.store.reload();
		 	    }                
                next.setDisabled(false);
            } else if (direction == -1) { 
            	// 上一步
                wizard.setActiveItem(0);
                prev.setDisabled(true);
            }
            
        } else if (activeId == 'card-2') {
        	var obj = Ext.getCmp($config.children[3].gridPanel.id);
        	// 下一步
            if (direction == 1 && this.doChecked(obj)) {
            	var nextPanel1 = Ext.getCmp($config.children[4].gridPanel.id);
            	var nextPanel2 = Ext.getCmp($config.children[5].gridPanel.id);
            	var tempId  = nextPanel1.store.baseParams.id;
            	Ext.apply(nextPanel1.store.baseParams,_this.postData);
            	Ext.apply(nextPanel2.store.baseParams,_this.postData);
            	
                wizard.setActiveItem(3);
 		 	    if (tempId != undefined  &&  tempId != _this.postData.id) {
		 	    	nextPanel1.store.reload();
		 	    	nextPanel2.store.reload();
		 	    }                    
                next.setText("完 成");
                next.setDisabled(false);
            } else if (direction == -1) { 
            	// 上一步
                wizard.setActiveItem(1);
                prev.setDisabled(false);
            }
        } else if (activeId == 'card-3') {
        	// 下一步
            if (direction == 1) {
	        	var obj = Ext.getCmp($config.children[5].gridPanel.id);
			 	if (obj.store.getCount()==='undefined' || obj.store.getCount() == 0) {
			 		Ext.Msg.alert('提示','请创建内容项!');
			 		return false;
			 	} else {
	                wizard.setActiveItem(0);
	                next.setText("下一步");
	                prev.setDisabled(true);
	                next.setDisabled(false);
			 	}	        	
            } else if (direction == -1) { 
            	// 上一步
                wizard.setActiveItem(2);
                next.setText("下一步");
                next.setDisabled(false);
            }
        }
    };
	

	
	var panel = new Ext.Panel({
		border              : false,
		id                  : 'pan',
		items               : [{
            id              : 'wizard',
            width:'100%',
            height          : app.getCenterH(),
            layout          : 'card',
            activeItem      : 0,
            deferredRender	: false,
            defaults        : {
                border      : false
            },
            bbar: [{
                id       : 'move-prev',
                text     : '上一步',
                handler  : navHandler.createDelegate(this, [-1]),
                disabled : true
            }, '->',{
                id       : 'move-next',
                text     : '下一步',
                handler  : navHandler.createDelegate(this, [1])
            }],
            items: [{
                id       : 'card-0',
                items    : [$config.children[0],$config.children[1]]
            }, {
                id       : 'card-1',
                items    : [$config.children[2]]
            }, {
                id       : 'card-2',
                items    : [$config.children[3]]
            }, {
                id       : 'card-3',
                items    : [$config.children[4],$config.children[5]]
            }]
        }]	
	});	
	
	
	var thisConfig = {
		closable   : false,
		autoScroll : true,
		frame      : false,
		items      : [panel]
	};
	var _config = $config || {};
	Ext.apply(_this, Ext.apply(thisConfig, _config));
	VAS.PropertyConfigWindow.superclass.constructor.call(_this);

};
Ext.extend(VAS.PropertyConfigWindow,VAS.BasicWindow,{
});


Ext.reg('propertyconfig-win',VAS.PropertyConfigWindow);

